import "babel-polyfill"
var beiMiCommon = require("BeiMiCommon");
var authClient = null; 
cc.Class({
    extends: beiMiCommon,
    // use this for initialization
    onLoad: async function ()  { 
        authClient =  await dfxcommon.aclient.create();     
    }, 
    login: async function(){
         
        /*(async () => {
            const result = await window.ic.plug.requestConnect();
            const connectionState = result ? "allowed" : "denied";
            console.log(`The Connection was ${connectionState}!`);
        })();  

        async () => {
            const result2 = await window.ic.plug.isConnected();
            console.log(`Plug connection is ${result2}`);
        })();*/
        const isauth = await authClient.isAuthenticated();
        const identity = await authClient.getIdentity();
        console.log("login..."+identity);
        (async () => {
            await authClient.login({
                onSuccess: async () => {
                this.handleAuthenticated(authClient);
                },
                identityProvider:"https://identity.ic0.app/#authorize",
                // process.env.DFX_NETWORK === "ic"
                //  ? "https://identity.ic0.app/#authorize"
                //   : process.env.LOCAL_II_CANISTER,
                // Maximum authorization expiration is 8 days
            });
        })();


        /*
        this.io = require("IOUtils"); 
        this.loadding(); 
        if(this.io.get("userinfo") == null){
            //发送游客注册请求
            var xhr = cc.beimi.http.httpGet("/api/guest", this.sucess , this.error , this);
        }else{ 
            //通过ID获取 玩家信息
            var data = JSON.parse(this.io.get("userinfo")) ;
            if(data.token != null){     //获取用户登录信息
                var xhr = cc.beimi.http.httpGet("/api/guest?token="+data.token.id, this.sucess , this.error , this);
            }
        }*/
	},
    handleAuthenticated:async function(authClient) {
        // const identity = await authClient.getIdentity();
        // const whoami_actor = createActor(canisterId as string, {
        //   agentOptions: {
        //     identity,
        //   },
        // });
      
        // renderLoggedIn(whoami_actor, authClient);
        const isauth = await authClient.isAuthenticated();
        const identity = await authClient.getIdentity();
        console.log("callback..."+identity);
      },
    sucess:function(result , object){
        var data = JSON.parse(result) ;
        if(data!=null && data.token!=null && data.data!=null){
            //放在全局变量
            object.reset(data , result);
            cc.beimi.gamestatus = data.data.gamestatus ;
            /**
             * 登录成功后即创建Socket链接
             */
            object.connect();
            //预加载场景
            if(cc.beimi.gametype!=null && cc.beimi.gametype != ""){//只定义了单一游戏类型 ，否则 进入游戏大厅
                object.scene(cc.beimi.gametype , object) ;
            }else{
                /**
                 * 暂未实现功能
                 */
            }
        }
    },
    error:function(object){
        object.closeloadding(object.loaddingDialog);
        object.alert("网络异常，服务访问失败");
    }

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
